# 未登录界面
def nologin_option():
    while True:
        print('输入:"1"登录;"2"注册;"3"商品信息;"exit"退出系统')
        my_option = input("请输入选择(1、2、3、exit)：")
        if my_option == "1":
            login()
            break
        elif my_option == "2":
            zhuce()
            break
        elif my_option == "3":
            look()
            continue
        elif my_option == "exit":
            my_exit()
        else:
            print("格式不正确！")
            continue

# 登录后
def already_login_option():
    while True:
        print('输入:"1"商品信息;"2"购物;"3"购买记录;"4"余额;"exit"退出系统')
        my_option = input("请输入系统用户选择(1-4、exit)：")
        if my_option == "1":
            look()
            continue
        elif my_option == "2":
            look()
            shopping_car()
            confirm_pay()
            break
        elif my_option == "3":
            show_buy_history()
            continue
        elif my_option == "4":
            print(f"用户余额(元):{my_money}")
            continue
        elif my_option == "exit":
            my_exit()
        else:
            print("格式不正确！")
            continue

# 登录
def login():
    while True:
        username = input("请输入用户名：")
        passwd = input("请输入密码：")
        if (username, passwd) in user_table.items():
            print("登录成功！")
            break
        else:
            print("账号错误！")
            _ = input("是否返回? (Y/others):")
            if _ == "Y":
                nologin_option()
                break
            else:
                continue

# 注册
def zhuce():
    while True:
        username = input("请输入用户名：")
        if username in user_table:
            print("用户名已存在！")
            continue
        else:
            passwd = input("请输入密码：")
            user_table[username] = passwd
            print("注册成功！")
            break

# 输入预算
def budget():
    from decimal import Decimal
    global budget_money
    budget_money = Decimal(input("请输入预算(元)："))

# 商品信息
def look():
    print("商品信息")
    for i in goods_table.items():
        print(f"商品编号:{i[0]}，名称:{i[1]['name']}，价格:{i[1]['price']}元/斤")

# 选择商品(未付款)
def shopping_car():
    global num                                  # 商品编号
    global amount                               # 购买数量
    while True:
        num = input("请输入购买的商品编码：")
        if num not in goods_table:
            print("编号不存在！")
            _ = input("是否退出? (Y/others):")
            if _ == "Y":
                already_login_option()
                break
            else:
                continue
        else:
            amount = input("请输入数量(斤)：")
            if num in shop_car:
                shop_car[num] += amount
            else:
                shop_car[num] = amount
            # break

# 计算购物金额
def calculate_pay():
    from decimal import Decimal
    global pay_money
    global goods_price
    goods_price = goods_table[num]['price']
    pay_money = Decimal(goods_price * float(amount))
    pay_money = round(pay_money, 2)
    print(f"所需金额:{pay_money}元")

# 检测余额/付款
def confirm_pay():
    global my_money
    global goods_pay_num
    calculate_pay()
    confirm = input("确认付款? (Y/others):")
    if confirm == "Y":
        if my_money >= pay_money:
            my_money = round(my_money - pay_money, 2)
            goods_pay_num += 1
            buy_history_add()
            print(f"购买成功，当前账户还剩{my_money}元。")
            success_pay = input("继续购买? (Y/others):")
            if success_pay == "Y":
                look()
                shopping_car()
                confirm_pay()
            else:
                already_login_option()
        else:
            print("余额不足")
            no_enough_money = input("重新购买? (Y/others):")
            if no_enough_money == "Y":
                look()
                shopping_car()
                confirm_pay()
            else:
                already_login_option()
    else:
        print("购买失败")
        already_login_option()

# 购买记录追加
def buy_history_add():
    global goods_name
    goods_name = goods_table[num]['name']
    buy_log.append([goods_pay_num, num, goods_name, goods_price, amount, pay_money])

def show_buy_history():
    print("购买记录:")
    for i in buy_log:
        print(f"NO:{i[0]},编号:{i[1]},名称:{i[2]},单价:{i[3]}元/斤,数量:{i[4]}斤,总价:{pay_money}元")

def my_exit():
    show_buy_history()
    print(f"账户余额 {my_money}元")
    print("退出系统")
    exit()

    # user_table = {'root': '123456', 'admin': 'admin'}
    # goods_table = {'F00001': {'name': '苹果', 'price': 1.2},
    #                'F00002': {'name': '香蕉', 'price': 5.5}}

shop_car = {}
def shopping_system():
    global user_table
    global goods_table
    global buy_log
    global my_money
    global goods_pay_num
    user_table = {'root': '123456', 'admin': 'admin'}
    goods_table = {'F00001': {'name': '苹果', 'price': 1.2},
                   'F00002': {'name': '香蕉', 'price': 5.5}}
    buy_log = list()        # 初始账单
    my_money = 0            # 初始余额
    nologin_option()        # 未登录
    budget()                # 预算
    my_money = budget_money # 余额
    goods_pay_num = 0       # 订单编号
    already_login_option()  # 选择窗口2

shopping_system()

